package org.buptsse.thread;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ConnectionChecker;
import org.buptsse.utils.HttpHelper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class SendMessageThread extends Thread {

	
	private Context context;
	private Handler handler;
	private String message,sender,receiver,makeOrder,sellid;
	
	
	public SendMessageThread(Context context,Handler handler,String message, String sender,String receiver,String makeOrder)
	{
		this.context=context;
		this.handler=handler;
		this.message=message;
		this.sender=sender;
		this.receiver=receiver;
		this.makeOrder=makeOrder;
	}
	public SendMessageThread(Context context,Handler handler,String message, String sender,String receiver,String makeOrder,String sellid)
	{
		this.context=context;
		this.handler=handler;
		this.message=message;
		this.sender=sender;
		this.receiver=receiver;
		this.makeOrder=makeOrder;
		this.sellid=sellid;
	}
	
	public void run()
	{
		if(ConnectionChecker.CheckConnection(context, handler)==false)
		{
			return;
		}
		ArrayList<NameValuePair> param=new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("message", message));
		param.add(new BasicNameValuePair("sender", sender));
		param.add(new BasicNameValuePair("receiver", receiver));
		System.out.println("is equals:"+makeOrder.equals("true"));
		if(makeOrder.equals("true")==true)
		{
			param.add(new BasicNameValuePair("makeorder", "true"));
			param.add(new BasicNameValuePair("sellid", sellid));
		}
		try {
			String result=HttpHelper.sendPost(APPConstant.sendMessageUrl, param);
			
			if(result.contains("success"))
			{
				//成功
				Message msg=handler.obtainMessage(APPConstant.sendMessageFinish);
				//System.out.println("SendMessageThread:"+result);
				handler.sendMessage(msg);
				return;
			}else{
				//失败
				Message msg=handler.obtainMessage(APPConstant.sendMessageError);
				handler.sendMessage(msg);
				return;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
